$(function() {
    $("#jqGrid").jqGrid({
        url: baseURL + 'application/controller/list.php',
        datatype: "json",
        colModel: [
            { label: 'pid', name: 'pid', index: 'pid', width: 80, key: true, hidden: true },
            { label: '车牌号码', name: 'idcard', index: 'idcard', width: 80 },
            { label: '内容', name: 'sys_content', index: 'sys_content', width: 80 },
            { label: '创建时间', name: 'sys_create_time', index: 'sys_create_time', width: 80 },
            { label: '最后一次修改', name: 'last_modify_time', index: 'last_modify_time', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function() {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        carts: {}
    },
    methods: {
        query: function() {
            vm.reload();
        },
        add: function() {
            vm.showList = false;
            vm.title = "新增";
            vm.carts = {};
        },
        update: function(event) {
            var pid = getSelectedRow();
            if (pid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(pid)
        },
        saveOrUpdate: function(event) {
            var url = vm.carts.pid == null ? "application/controller/save.php" : "application/controller/update.php";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.carts),
                success: function(r) {
                    if (r.code === 0) {
                        alert('操作成功', function(index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function(event) {
            var pids = getSelectedRows();
            if (pids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function() {
                $.ajax({
                    type: "POST",
                    url: baseURL + "application/controller/del.php",
                    contentType: "application/json",
                    data: JSON.stringify(pids),
                    success: function(r) {
                        if (r.code == 0) {
                            alert('操作成功', function(index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        search: function() {
            $("#jqGrid").jqGrid('setGridParam', {
                url: baseURL + 'application/controller/list.php',
                postData: { 'search': $("#search").val() }, //发送数据 
                page: 1
            }).trigger("reloadGrid"); //重新载入 
        },
        getInfo: function(pid) {
            $.get(baseURL + "application/controller/info.php?pid=" + pid, function(r) {
                vm.carts = r.carts;
            });
        },
        reload: function(event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});